<?php

namespace app\admin\model;

use app\model\Model;

class Channel extends Model
{
  public $privilege = 'channel';
  public $cids;
  public $id;
  public $priv;
  public $channel_list;
  public $db;

  function __construct()
  {
    include 'check.php';
    new Check();

    $this->db = $GLOBALS['db'];
    $this->cids = checkAdminPriv(@$this->privilege); // 判断权限
    $this->id = intval($_GET['id']);
    $this->priv = priv_id($GLOBALS['db']->getOne("SELECT r.r_priv FROM user AS u INNER JOIN role AS r ON r.id=u.u_rid WHERE u.id = " . get_admin_token('id')));
  }

  function index()
  {
    $this->channel_list = moduleAdminReplace(file_get('html', 'channel_list'));

    $this->tpl('channel', [
      'db' => $this->db
    ]);
    /*
    if (!$this->channel_list) {
      file_it(channel_list($this->priv), 'html', 'channel_list');
      $this->channel_list = file_get('html', 'channel_list');
    }
    */
  }

  function add()
  {
    $this->tpl('channel_add', [
      'db' => $this->db
    ]);
  }

  function add_action()
  {
    $data = get_post(['c_picture', 'c_content',  'c_scontent', 'c_nname', 'c_link', 'c_sname', 'c_aname', 'c_cover', 'c_slideshow', 'c_seoname', 'c_keywords', 'c_description'], false);
    unset($data['c_cmodel_select']);
    unset($data['c_dmodel_select']);

    $data['c_ifpicture'] = !empty($data['c_picture']) ? 1 : 0;
    $data['c_ifcover'] = !empty($data['c_cover']) ? 1 : 0;
    $data['c_ifslideshow'] = !empty($data['c_slideshow']) ? 1 : 0;

    if ($id = $GLOBALS['db']->exec("INSERT INTO channel " . arr_insert($data))) {
      \admin_log('频道新增 [id:' . $id . ']');
      update_channel();
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'channel'));
    }

    alert_back($GLOBALS['lang']['msg_tryagain']);

    /*
    $data['c_name'] = str_safe($_POST['c_name']);
    $data['c_picture'] = str_safe($_POST['c_picture']);
    $data['c_ifpicture'] = !empty($data['c_picture']) ? 1 : 0;
    $data['c_parent'] = str_safe($_POST['c_parent']);
    $data['c_cmodel'] = str_safe($_POST['c_cmodel']);
    $data['c_dmodel'] = str_safe($_POST['c_dmodel']);
    $data['c_rec'] = iif($_POST['c_rec'], 0);
    $data['c_content'] = str_isafe($_POST['c_content']);
    $data['c_scontent'] = str_isafe($_POST['c_scontent']);
    $data['c_page'] = intval($_POST['c_page']);
    $data['c_seoname'] = str_safe($_POST['c_seoname']);
    $data['c_keywords'] = str_safe($_POST['c_keywords']);
    $data['c_description'] = str_safe($_POST['c_description']);
    $data['c_navigation'] = iif($_POST['c_navigation'], 0);
    $data['c_nname'] = $_POST['c_nname'] == '' ? str_safe($data['c_name']) : str_safe($_POST['c_nname']);
    $data['c_link'] = str_safe($_POST['c_link']);
    $data['c_sname'] = str_safe($_POST['c_sname']);
    $data['c_aname'] = str_safe($_POST['c_aname']);
    $data['c_cover'] = str_safe($_POST['c_cover']);
    $data['c_ifcover'] = !empty($data['c_cover']) ? 1 : 0;
    $data['c_slideshow'] = !empty($_POST['c_slideshow']) ? str_safe($_POST['c_slideshow']) : '';
    $data['c_ifslideshow'] = !empty($data['c_slideshow']) ? 1 : 0;
    $data['c_target'] = str_safe($_POST['c_target']);
    $data['c_safe'] = iif($_POST['c_safe'], 0);
    $data['c_order'] = intval($_POST['c_order']);

    null_back($data['c_name'], '请填写频道名称');
    n_back($data['c_parent'], '请选择上级频道');
    null_back($data['c_cmodel'], '请选择或填写频道模型');
    null_back($data['c_dmodel'], '请选择或填写详情模型');
    non_numeric_back($data['c_page'], '分页条数必须是数字');
    non_numeric_back($data['c_order'], '排序必须是数字');
    */
  }

  function edit()
  {
    $privilege = 'c' . $this->id;

    $this->tpl('channel_edit', [
      'db' => $this->db,
      'row' => $GLOBALS['db']->getRow("SELECT c_name,c_picture,c_parent,c_cmodel,c_dmodel,c_content,c_scontent,c_page,c_order,c_navigation,c_nname,c_link,c_sname,c_aname,c_cover,c_slideshow,c_target,c_safe,c_seoname,c_keywords,c_description FROM channel WHERE id = $this->id"),
    ]);
  }

  function edit_action()
  {
    $data = get_post(['c_picture', 'c_content',  'c_scontent', 'c_nname', 'c_link', 'c_sname', 'c_aname', 'c_cover', 'c_slideshow', 'c_seoname', 'c_keywords', 'c_description'], false);
    unset($data['c_cmodel_select']);
    unset($data['c_dmodel_select']);

    $data['c_ifpicture'] = !empty($data['c_picture']) ? 1 : 0;
    $data['c_picture'] = !empty($data['c_picture']) ? $data['c_picture'] : '';
    $data['c_ifcover'] = !empty($data['c_cover']) ? 1 : 0;
    $data['c_cover'] = !empty($data['c_cover']) ? $data['c_cover'] : '';
    $data['c_ifslideshow'] = !empty($data['c_slideshow']) ? 1 : 0;
    $data['c_slideshow'] = !empty($data['c_slideshow']) ? $data['c_slideshow'] : '';
    $data['c_content'] = !empty($data['c_content']) ? $data['c_content'] : '';
    $data['c_scontent'] = !empty($data['c_scontent']) ? $data['c_scontent'] : '';
    $data['c_nname'] = !empty($data['c_nname']) ? $data['c_nname'] : '';
    $data['c_link'] = !empty($data['c_link']) ? $data['c_link'] : '';
    $data['c_sname'] = !empty($data['c_sname']) ? $data['c_sname'] : '';
    $data['c_aname'] = !empty($data['c_aname']) ? $data['c_aname'] : '';
    $data['c_cover'] = !empty($data['c_cover']) ? $data['c_cover'] : '';
    $data['c_seoname'] = !empty($data['c_seoname']) ? $data['c_seoname'] : '';
    $data['c_keywords'] = !empty($data['c_keywords']) ? $data['c_keywords'] : '';
    $data['c_description'] = !empty($data['c_description']) ? $data['c_description'] : '';

    if ($id = $GLOBALS['db']->exec("UPDATE cms_channel SET " . arr_update($data) . " WHERE id= $this->id")) {
      \admin_log("频道编辑 [id:$this->id]");
      update_channel();
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'channel'));
    }

    alert_back($GLOBALS['lang']['msg_tryagain']);

    /*
    $data['c_name'] = str_safe($_POST['c_name']);
    $data['c_picture'] = str_safe($_POST['c_picture']);
    $data['c_ifpicture'] = !empty($data['c_picture']) ? 1 : 0;
    $data['c_parent'] = str_safe($_POST['c_parent']);
    $data['c_cmodel'] = str_safe($_POST['c_cmodel']);
    $data['c_dmodel'] = str_safe($_POST['c_dmodel']);
    $data['c_rec'] = intval($_POST['c_rec']);
    $data['c_content'] = str_isafe($_POST['c_content']);
    $data['c_scontent'] = str_isafe($_POST['c_scontent']);
    $data['c_page'] = intval($_POST['c_page']);
    $data['c_seoname'] = str_safe($_POST['c_seoname']);
    $data['c_keywords'] = str_safe($_POST['c_keywords']);
    $data['c_description'] = str_safe($_POST['c_description']);
    $data['c_navigation'] = intval($_POST['c_navigation']);
    $data['c_nname'] = str_safe($_POST['c_nname']);
    $data['c_link'] = str_safe($_POST['c_link']);
    $data['c_sname'] = str_safe($_POST['c_sname']);
    $data['c_aname'] = str_safe($_POST['c_aname']);
    $data['c_cover'] = str_safe($_POST['c_cover']);
    $data['c_ifcover'] = !empty($data['c_cover']) ? 1 : 0;
    $data['c_slideshow'] = !empty($_POST['c_slideshow']) ? str_safe($_POST['c_slideshow']) : '';
    $data['c_ifslideshow'] = !empty($data['c_slideshow']) ? 1 : 0;
    $data['c_target'] = str_safe($_POST['c_target']);
    $data['c_safe'] = intval($_POST['c_safe']);
    $data['c_order'] = intval($_POST['c_order']);

    null_back($data['c_name'], '请填写频道名称');
    n_back($data['c_parent'], '请选择上级频道');
    null_back($data['c_cmodel'], '请选择或填写频道模型');
    null_back($data['c_dmodel'], '请选择或填写详情模型');
    non_numeric_back($data['c_page'], '分页条数必须是数字');
    non_numeric_back($data['c_order'], '排序必须是数字');
    */
  }

  function del()
  {
    $res = $GLOBALS['db']->getRow("SELECT * FROM channel WHERE id = $this->id");

    if ($res['c_ifsub'] == 0 && $res['c_safe'] == 0) {
      unset($res);
      // 频道相关清理
      $GLOBALS['db']->exec("DELETE FROM detail WHERE d_parent = $this->id");
      $GLOBALS['db']->exec("DELETE FROM channel WHERE id = $this->id");
      update_channel();
      admin_log('频道删除 [id:' . $this->id . ']');
      href(M('admin', 'channel'));
    }

    alert_back('此频道存在下级或已受保护，无法删除！');
  }

  function update()
  {
    update_channel();
    back();
  }

  function __destruct()
  {
  }
}
